
#include <asm.h>

.global C_SYMBOL(RGB24StretchBlit2RGB24_Raw)

C_SYMBOL(RGB24StretchBlit2RGB24_Raw):

srcy_16= -0x48
interpolation= -0x44
yrIntFloat_16= -0x40
srcHeight= -0x3C
dstPitch= -0x38
dstHeight= -0x34
dstWidth= -0x30
dstPt= -0x2C
RestWidth= -0x28
LineSrcPt= -0x24
srcPitch= -0x20
v8 = -0x1C
xrIntFloat_16 = -0x18
srcWidth= -0x14
srcPt= -0x10

    MOV     R12, SP                 // R12: argument
    STMFD	SP!, {R4-R12,LR}
    SUB     SP, SP, #0x4C
Start:
//.long 0xe7fddefe

    LDMIA   R12, {R4-R10}
    STR     R0, [SP,#0x4C+srcPt]
    STR     R1, [SP,#0x4C+dstPt]
    STR     R2, [SP,#0x4C+srcPitch]
    STR     R3, [SP,#0x4C+dstPitch]

    STR     R4, [SP,#0x4C+srcWidth]
    STR     R5, [SP,#0x4C+srcHeight]
    STR     R6, [SP,#0x4C+dstWidth]

    STR     R7, [SP,#0x4C+dstHeight]

    STR     R8, [SP,#0x4C+xrIntFloat_16]
    STR     R9, [SP,#0x4C+yrIntFloat_16]
    STR     R10, [SP,#0x4C+interpolation]

    SUB     R6, R3, R6, LSL #2       //dstPitch - dstWidth*4;
    STR     R6, [SP,#0x4C+RestWidth]

    MOV     R11, R2
    MOV     R7, R8
    MOV     R9, R1

    LDR LR,=0x00FF00FF

    MOV	R3, #0

    STR	R3, [SP,#0x4C+srcy_16]

    LDR	R3, [SP,#0x4C+dstHeight]
    CMP	R3, #0
    BLS	Done

Header:

    LDR	R1, [SP,#0x4C+srcy_16]
    MOV	R3, R1,	LSL#16
    MOV R8, R3, LSR#24
    STR R8, [SP,#0x4C+v8]

    MOV	R2, R1,LSR#16

    LDR R3, [SP,#0x4C+srcPt]
    MLA	R4, R2,	R11, R3
    STR R4, [SP,#0x4C+LineSrcPt]

    LDR	R10, [SP,#0x4C+dstWidth]

    MOV R12, #0
    CMP	R10, #0

    MOV R0, R7, LSR #14

    BLE Done

    LDR     R1, [SP,#0x4C+interpolation]
    ANDS   R1,  R1, #1

    MOV	    R2, R12,LSR#16

    BEQ   LoopX1

LoopX0:

    LDR R4, [SP,#0x4C+LineSrcPt]
    LDR R11,[SP,#0x4C+srcPitch]
    LDR R8, [SP,#0x4C+v8]

    MOV	R2, R12, LSR#16
    MOV	R3, R12, LSL#16

    ADD R0, R4, R2, LSL #2
    ADD R1, R0, R11
    MOV	R3, R3,  LSR#24

    LDR R7, [R0], #4
    LDR R0, [R0]

    MUL R5, R3, R8
	MOV R5, R5, LSR #8
    RSB R6, R8, #256
    ADD R6, R6, R5
    SUB R6, R6, R3

	AND R8, LR, R7
	MUL R11, R8, R6
	AND R7, LR, R7, LSR #8
	MUL R4, R7, R6

    SUB R6, R3, R5
	AND R8, R0, LR
	MLA R11, R8, R6, R11
	AND R7, LR, R0, LSR #8
    LDR R3, [SP,#0x4C+v8]
    LDR R0, [SP,#0x4C+xrIntFloat_16]
	MLA R4, R7, R6, R4

	LDR R2, [R1], #4
	LDR R1, [R1]

    SUB R6, R3, R5
	AND R7, LR, R2, LSR #8
	MLA R4, R7, R6, R4
	AND R8, R2, LR
	MLA R11, R8, R6, R11

	AND R7, LR, R1, LSR #8
	MLA R4, R7, R5, R4
	AND R8, R1, LR
	MLA R11, R8, R5, R11

	AND R4, LR, R4, LSR#8
	AND R11, LR, R11, LSR#8
	ORR R11, R11, R4, LSL#8
    ADD R12, R12, R0
	SUBS R10, R10, #1
    STR R11, [R9], #4

    BGT	LoopX0

NextLine0:

    LDR	R1, [SP,#0x4C+srcy_16]
    LDR	R2, [SP,#0x4C+yrIntFloat_16]
    LDR R6, [SP,#0x4C+RestWidth]
    LDR R11,[SP,#0x4C+srcPitch]
    ADD	R1, R1,	R2
    STR	R1, [SP,#0x4C+srcy_16]

    MOV	R3, R1, LSL#16
    MOV R8, R3, LSR#24

    MOV	R2, R1,LSR#16

    LDR R3, [SP,#0x4C+srcPt]
    ADD R9, R9, R6
    MLA	R4, R2,	R11, R3

    LDR	R10, [SP,#0x4C+dstWidth]
    LDR	R2, [SP,#0x4C+dstHeight]


    MOV R12, #0

    SUBS R2, R2, #1
    STR R4, [SP,#0x4C+LineSrcPt]
    STR	R2, [SP,#0x4C+dstHeight]
    STR R8, [SP,#0x4C+v8]

    BGT  LoopX0
    B   Done

LoopX1:
@.long 0xe7fddefe

    ADD	    R2, R4, R2, LSL #2
    SUBS R10, R10, #1
    LDR R1, [R2]
    ADD R12, R12, R7
    MOV R2, R12,LSR#16
    STR	R1, [R9], #4

    BGT	LoopX1

NextLine1:

    LDR	R1, [SP,#0x4C+srcy_16]
    LDR	R2, [SP,#0x4C+yrIntFloat_16]
    ADD	R1, R1,	R2
    STR	R1, [SP,#0x4C+srcy_16]

    MOV	R3, R1, LSL#16
    MOV R8, R3, LSR#24

    MOV	R2, R1,LSR#16

    LDR R3, [SP,#0x4C+srcPt]
    ADD R9, R9, R6
    MLA	R4, R2,	R11, R3

    LDR	R10, [SP,#0x4C+dstWidth]
    LDR	R2, [SP,#0x4C+dstHeight]

    MOV R12, #0

    SUBS R2, R2, #1
    STR	R2, [SP,#0x4C+dstHeight]

    MOV R2, R12,LSR#16

    BGT  LoopX1

Done:
    ADD	    SP, SP,	#0x4C
    LDMFD   SP, {R4-R11,SP,PC}

